﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarBlackBox
{
    public partial class Form_DriverEditor : Form
    {
        private DriverInfoData Driver = new DriverInfoData();
        bool bClose = true;
        Form_DriverManage F;
        public Form_DriverEditor(Form_DriverManage f)
        {
            InitializeComponent();
            F = f;
        }
        public Form_DriverEditor(DriverInfoData d)
        {
            InitializeComponent();
            Driver = d;
        }
        private void btCancel_Click(object sender, EventArgs e)
        {
            if (WFNetLib.WFGlobal.MessageYseOrNo("确定取消当前操作么？", "驾驶员信息编辑取消") == DialogResult.No)
                bClose = false;
        }

        private void btEnter_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Driver.DIN))//添加
            {
                if (GetDriverInfo())
                {
                    int n = 0;
                    n = driverInfoTableAdapter.Insert(Driver.DIN,
                                                    Driver.DriverID,
                                                    Driver.Name,
                                                    Driver.Sex,
                                                    Driver.LastUpdateTime,
                                                    Driver.Operator,
                                                    Driver.HasSet,
                                                    Driver.Memo,
                                                    Driver.Phone,
                                                    Driver.Address,
                                                    Driver.StartWorkDate,
                                                    Driver.DriveAge,
                                                    Driver.CarType,
                                                    Driver.LicensePlate,
                                                    Driver.HoldDate,
                                                    Driver.ValidDate);
                    if (n != 1)
                        MessageBox.Show("存储失败，请重新尝试或联系管理员!!!!");
                    else
                    {
                        F.RenovateDriverInfo();
                        WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                        log.LogType = LogType.驾驶员管理.ToString();
                        log.LogContent = "添加驾驶证号为" + Driver.DIN + "的驾驶员资料";
                        log.LogRemark = "驾驶员添加";
                        log.InserSQLLog();
                        if (WFNetLib.WFGlobal.MessageYseOrNo("存储成功\r\nYes继续添加\r\nNo退出添加", "成功") == DialogResult.Yes)
                        {
                            Driver = new DriverInfoData();
                            SetDriverInfo();
                        }
                        else
                        {
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                }
            }
            else//修改
            {
                if (GetDriverInfo())
                {
                    int n = 0;
                    n = driverInfoTableAdapter.UpdateQuery(
                                                    Driver.DriverID,
                                                    Driver.Name,
                                                    Driver.Sex,
                                                    Driver.LastUpdateTime,
                                                    Driver.Operator,
                                                    Driver.HasSet,
                                                    Driver.Memo,
                                                    Driver.Phone,
                                                    Driver.Address,
                                                    Driver.StartWorkDate,
                                                    Driver.DriveAge,
                                                    Driver.CarType,
                                                    Driver.LicensePlate,
                                                    Driver.HoldDate,
                                                    Driver.ValidDate,
                                                    Driver.DIN);
                    if (n != 1)
                        MessageBox.Show("存储失败，请重新尝试或联系管理员!!!!");
                    else
                    {
                        MessageBox.Show("修改成功!");
                        WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                        log.LogType = LogType.驾驶员管理.ToString();
                        log.LogContent = "修改驾驶证号为" + Driver.DIN + "的驾驶员资料";
                        log.LogRemark = "驾驶员修改";
                        log.InserSQLLog();
                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
            //this.DialogResult = DialogResult.OK;
        }

        private void Form_DriverEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!bClose)
            {
                e.Cancel = true;
                bClose = true;
            }
        }

        private void Form_DriverEditor_Load(object sender, EventArgs e)
        {            
            // TODO: 这行代码将数据加载到表“systemDBDataSet.CarType”中。您可以根据需要移动或移除它。
            this.carTypeTableAdapter.Fill(this.systemDBDataSet.CarType);
            // TODO: 这行代码将数据加载到表“systemDBDataSet.VehicleInfo”中。您可以根据需要移动或移除它。
            this.vehicleInfoTableAdapter.FillShow(this.systemDBDataSet.VehicleInfo);
            cbLicensePlate.Text = "";
            cbCarType.Text = "";
            cbSex.SelectedIndex = 0;
            if (!string.IsNullOrEmpty(Driver.DIN))//非空为更改
            {
                tbDIN.ReadOnly = true;
                SetDriverInfo();
                this.Text = this.Text + "修改" + "" + Driver.DIN + "驾驶员资料";
            }
            else
                this.Text = this.Text + "新增驾驶员资料";
        }
        bool GetDriverInfo()
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                tbName.Focus();
                WFNetLib.WFGlobal.MessageWaring("驾驶员姓名不能为空", "缺少关键参数");
                return false;
            }
            if (string.IsNullOrEmpty(tbDIN.Text))
            {
                tbDIN.Focus();
                WFNetLib.WFGlobal.MessageWaring("驾驶证编号不能为空", "缺少关键参数");
                return false;
            }
            Driver.Name = tbName.Text;
            Driver.DriverID = Convert.ToInt32(nDriverID.Value);
            Driver.Sex = cbSex.Text;
            Driver.Phone = tbPhone.Text;
            Driver.DIN = tbDIN.Text;
            Driver.ValidDate = Convert.ToInt16(nValidDate.Value);
            if (dtpHoldDate.Checked)
                Driver.HoldDate = dtpHoldDate.Value.ToLongDateString();
            else
                Driver.HoldDate = "";
            Driver.LicensePlate = cbLicensePlate.Text;
            Driver.CarType = cbCarType.Text;
            Driver.DriveAge = Convert.ToInt16(nDriveAge.Value);
            if (dtpStartWorkDate.Checked)
                Driver.StartWorkDate = dtpStartWorkDate.Value.ToLongDateString();
            else
                Driver.StartWorkDate = "";
            Driver.Address = tbAddress.Text;
            Driver.Memo = WFNetLib.Strings.StringsFunction.strEncode(tbMemo.Text);
            Driver.Operator = Form_Main.Operator;
            return true;
        }
        void SetDriverInfo()
        {
            tbName.Text = Driver.Name;
            nDriverID.Value = Convert.ToDecimal(Driver.DriverID);
            cbSex.Text = Driver.Sex;
            tbPhone.Text = Driver.Phone;
            tbDIN.Text = Driver.DIN;
            nValidDate.Value = Convert.ToDecimal(Driver.ValidDate);
            try
            {
                dtpHoldDate.Value = Convert.ToDateTime(Driver.HoldDate);
                dtpHoldDate.Checked = true;
            }
            catch
            {
                dtpHoldDate.Checked = false;
            }
            cbLicensePlate.Text = Driver.LicensePlate;
            cbCarType.Text = Driver.CarType;
            nDriveAge.Value = Convert.ToDecimal(Driver.DriveAge);
            try
            {
                dtpStartWorkDate.Value = Convert.ToDateTime(Driver.StartWorkDate);
                dtpStartWorkDate.Checked = true;
            }
            catch
            {
                dtpStartWorkDate.Checked = false;
            }
            tbMemo.Text = WFNetLib.Strings.StringsFunction.strDecode(Driver.Memo);
            tbOperator.Text = Driver.Operator;
            tbLastUpdateTime.Text = Driver.LastUpdateTime.ToString();
        }
    }
}
